package com.atguigu.gmall.controller;

import com.atguigu.gmall.client.service.SeckillFeignClientService;
import com.atguigu.gmall.client.service.UserFeignClientService;
import com.atguigu.gmall.entity.SeckillProduct;
import com.atguigu.gmall.entity.UserAddress;
import com.atguigu.gmall.result.RetVal;
import com.atguigu.gmall.util.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class WebSeckillController {

    @Autowired
    private SeckillFeignClientService seckillFeignClientService;

    @GetMapping("/seckill-index.html")
    public String seckillIndex(Model model) {
        RetVal retVal = seckillFeignClientService.queryAllSeckillProduct();
        model.addAttribute("list", retVal.getData());
        return "seckill/index";
    }

    //2.根据skuId 获取秒杀对象数据
    @GetMapping("/seckill-detail/{skuId}.html")
    public String seckillDetail(@PathVariable Long skuId, Model model) {
        //获取商品详情数据
        RetVal result = seckillFeignClientService.getSeckillProductBySkuId(skuId);
        model.addAttribute("item", result.getData());
        return "seckill/detail";
    }

    //3.获取下单码成功之后要访问的页面
    @GetMapping("/seckill-queue.html")
    public String seckillQueue(Long skuId, String seckillCode, HttpServletRequest request){
        request.setAttribute("skuId",skuId);
        request.setAttribute("seckillCode",seckillCode);
        return "seckill/queue";
    }

    //4.秒杀确认订单页面
    @GetMapping("/seckill-confirm.html")
    public String seckillConfirm(Model model) {
        RetVal retVal = seckillFeignClientService.seckillConfirm();
        if (retVal.isOk()) {
            Map<String, Object> data = (Map<String, Object>) retVal.getData();
            model.addAllAttributes(data);
        }else{
            model.addAttribute("message",retVal.getMessage());
        }
        return "seckill/confirm";
    }
}
